﻿using B.S.Sterilizationcenter.Domain.yjb;
using B.S.Sterilizationcenter.Domain.zmc;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.Yjb.ConsumableMaterials;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.Yjb.ConsumableMaterials
{
    public class DelConsumableMaterialCommandHandlers : IRequestHandler<DelConsumableMaterialCommand, ApiResult<int>>
    {
        private readonly ISqlSugarClient db;

        public DelConsumableMaterialCommandHandlers(ISqlSugarClient db)
        {
            this.db = db;
        }
        public async Task<ApiResult<int>> Handle(DelConsumableMaterialCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> result = new ApiResult<int>();

                var del = await db.Queryable<ConsumableMaterial>().FirstAsync(p => p.ConsumableId == request.ConsumableId);
                del.IsDElete = true;
                var res = await db.Updateable<ConsumableMaterial>(del).ExecuteCommandAsync();
                if (res > 0)
                {
                    result.Code = ApiEnums.删除成功;
                    result.Msg = "删除成功";
                }
                else
                {
                    result.Code = ApiEnums.删除失败;
                    result.Msg = "删除失败";
                }
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
